package com.abner.cas.custom;

import com.cas.CustomPasswordEncoder;
import com.util.ResourceUtil;
import org.apereo.cas.authentication.*;
import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.services.ServicesManager;

import java.security.GeneralSecurityException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

/**
 * @author AbnerG
 * @version 1.0
 * @date 2019/11/14 19:27
 */
public class MyAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {

    public MyAuthenticationHandler(String name, ServicesManager servicesManager, PrincipalFactory principalFactory, Integer order) {
        super(name, servicesManager, principalFactory, order);
    }

    CustomPasswordEncoder customPasswordEncoder=new CustomPasswordEncoder();

    @Override
    public boolean supports(Credential credential) {
        //判断传递过来的Credential 是否是自己能处理的类型
        return credential instanceof UsernamePasswordCredential;
    }

    @Override
    protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential, String originalPassword) throws GeneralSecurityException, PreventedException {
        String username = credential.getUsername();
        String password = credential.getPassword();

        //MD5加密
        String encode = customPasswordEncoder.encode(password);

        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //直接是原生的数据库配置啊
            String url = ResourceUtil.getKey("url");
            String user = ResourceUtil.getKey("user");
            String pass = ResourceUtil.getKey("password");
            conn = DriverManager.getConnection(url,user, pass);
            //SQL
            String sql = ResourceUtil.getKey("sql");
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,encode);
            ResultSet rs = ps.executeQuery();
            System.out.println("------------------------->start"+username+"->"+password+"->"+encode);
            if (rs.next()){
                Map<String,Object> userinfo=new HashMap<>();
                String sqlinfo = ResourceUtil.getKey("sqlinfo");
                String[] split = sqlinfo.split("-");
                for (String info:split) {
                    userinfo.put(info,rs.getString(info));
                }
                System.out.println("end===================================");
                List<MessageDescriptor> warning = new ArrayList<MessageDescriptor>();
                return createHandlerResult(credential, this.principalFactory.createPrincipal(username, userinfo),warning);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
